package com.example.springbootesservice.controller;

import com.example.springbootesservice.common.*;
import com.example.springbootesservice.service.EsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Api(value = "ES测试", tags = "ES测试")
@RestController
@RequestMapping("/test")
public class EsApiController {

    @Autowired
    private EsApiService esApiService;

    @ApiOperation("测试连接")
    @GetMapping("/connect")
    public String testConnect() throws IOException {
        esApiService.test();
        return "success";
    }


    @ApiOperation("创建索引")
    @PostMapping("/create/index")
    public String testAdd(@RequestBody CreateIndexDto createIndexDto) throws IOException {
        esApiService.createIndex(createIndexDto);
        return "success";
    }

    @ApiOperation("添加文档")
    @PostMapping("/add/doc")
    public String testAdd(@RequestBody AddDocDto addDocDto) throws IOException {
        esApiService.addDoc(addDocDto);
        return "success";
    }

    @ApiOperation("更新文档")
    @PostMapping("/update/doc")
    public String testUpdate(@RequestBody UpdateDocDto updateDocDto) throws IOException {
        //esApiService.updateDoc(updateDocDto);
        esApiService.updateDoc2(updateDocDto);
        //esApiService.updateDoc3(updateDocDto);
        return "success";
    }


    @ApiOperation("删除文档")
    @PostMapping("/delete/doc")
    public String testDelete(@RequestBody DeleteDocDto deleteDocDto) throws IOException {
        //esApiService.deleteDoc(deleteDocDto);
        esApiService.deleteDoc2(deleteDocDto);
        return "success";
    }

    @ApiOperation("查询文档")
    @PostMapping("/search/doc")
    public String testSearch(@RequestBody SearchDocDto searchDocDto) throws IOException {
        switch (searchDocDto.getType()){
            case 1:esApiService.search(searchDocDto); break;
            case 2:esApiService.search2(searchDocDto); break;
            case 3:esApiService.search3(searchDocDto); break;
            case 4:esApiService.search4(searchDocDto); break;
            case 5:esApiService.search5(searchDocDto); break;
            case 6:esApiService.search6(searchDocDto); break;
            case 7:esApiService.search7(searchDocDto); break;
            case 8:esApiService.search8(searchDocDto); break;
            case 9:esApiService.search9(searchDocDto); break;
            case 10:esApiService.search10(searchDocDto); break;
            case 11:esApiService.search11(searchDocDto); break;
            case 12:esApiService.search12(searchDocDto); break;
            case 13:esApiService.search13(searchDocDto); break;
            case 14:esApiService.search14(searchDocDto); break;
            case 15:esApiService.search15(searchDocDto); break;
            case 16:esApiService.search16(searchDocDto); break;
            case 17:esApiService.search17(searchDocDto); break;
        }
        return "success";
    }
}
